//
//  SLXinViewController.m
//  SoccerLottery
//
//  Created by chuanshuangzhang chuan shuang on 15/12/24.
//  Copyright © 2015年 chuanshuangzhang. All rights reserved.
//

#import "SLXinViewController.h"
#import "SLFeedModel.h"
#import "SLXinFeedTableViewCell.h"
#import "SLChargeRecommandViewController.h"
#import "SLLoginViewController.h"
#import "SLChargeViewController.h"

@interface SLXinViewController ()<UIAlertViewDelegate>

@property (nonatomic,readwrite) SLFeedModel *currentModel;

@end

@implementation SLXinViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"SLXinFeedTableViewCell" bundle:nil] forCellReuseIdentifier:@"SLXinFeedTableViewCell"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadRecommend:@"2"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SLXinFeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLXinFeedTableViewCell" forIndexPath:indexPath];
    if(cell == nil){
        cell = [[SLXinFeedTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SLXinFeedTableViewCell"];
    }
    SLFeedModel *model = self.dataSorce[indexPath.section];
    cell.titleLabel.text = model.title;
    cell.scoreLabel.text = model.score;
    cell.timeLabel.text = [NSString stringWithFormat:@"%@",model.time];
    cell.priceLabel.text = [NSString stringWithFormat:@"价格：%@金币",model.price];
    cell.isPurchased = [model.status integerValue];
    [cell setResult:model.result];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _currentModel = (SLFeedModel *)self.dataSorce[indexPath.section];
    if([_currentModel.status integerValue]==1 || _currentModel.score.length > 0){
        SLChargeRecommandViewController *vc = [[SLChargeRecommandViewController alloc]init];
        vc.feedModel = _currentModel;
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"查看此推介将消耗%@个金币",_currentModel.price] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"查看", nil];
        [alterView show];
    }
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 100)
    {
        if(buttonIndex == 1){
            SLChargeViewController *vc = [[SLChargeViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        return;
    }
    if(buttonIndex == 1){
        [self openRecommand];
    }
}

- (void)openRecommand
{
    if(![VWebService isConnected]){
        [SVProgressHUD showErrorWithStatus:@"无网络链接，请检查您的网络"];
        return;
    }
    if([[UserModel sharedInstance].coin integerValue] < [_currentModel.price integerValue]){
        UIAlertView *alterV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"金币不足，请充值" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"充值", nil];
        alterV.tag = 100;
        [alterV show];
    }else {
        [UserModel sharedInstance].coin = [NSString stringWithFormat:@"%ld",[[UserModel sharedInstance].coin integerValue]-[_currentModel.price integerValue]];
        _currentModel.status = @"1";
        [SLDBManager saveFeed:_currentModel.feedId status:@"1"];
        SLChargeRecommandViewController *vc = [[SLChargeRecommandViewController alloc]init];
        vc.feedModel = _currentModel;
        [self.navigationController pushViewController:vc animated:YES];
        [self.tableView reloadData];
        if([UserModel sharedInstance].phone.length >= 11){
            [SLNewNetMgr setCoins:[UserModel sharedInstance].userId coins:[NSString stringWithFormat:@"-%@",_currentModel.price] recommandId:_currentModel.feedId block:^(id result, BOOL status, NSError *error) {
                
            }];
        }
    }
}
- (void)openLogin
{
    SLLoginViewController *vc = [[SLLoginViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
